using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Syncfusion.Windows.Forms.Grid;
using ctsu.BaseForms;
using ctsu.CustomControls;
using ctsu.Data;

namespace ctsu.Admin.BusLogic
{
	/// <summary>
	/// Summary description for BLFormGridAdminUsers.
	/// </summary>
	public class BLFormGridAdminUsersImpl : BLFormGridBaseImpl
	{
		//private DataTable dtDept;
		//private DataTable dtForm;

		public BLFormGridAdminUsersImpl(FormGridMeta formGridMeta, UserGrid uGrid, FormInfoBase formInfo, Form form)
			: base(formGridMeta, uGrid, formInfo, form)
		{
			Console.WriteLine("BLFormGridAdminUsersImpl constructor");
		}

		public override bool Initialize()
		{
			return base.Initialize();
		}

		protected override void DisplayRow(int row, DataRow dr)
		{
			for (int i = 1; i < this._uGrid.Grid.ColCount + 1; i++)
			{
				DataColumn dc = (DataColumn)this._uGrid.Grid[0, i].Tag;
				this._uGrid.Grid[row, i].CellValue = dr[dc.ColumnName];
				if (i == 3)
				{
					string cellVal = this._uGrid.Grid[row, 2].CellValue.ToString();
					DataTable dt = null;
					switch (cellVal)
					{
						case "-9":
							break;
						case "0":
							this._uGrid.Grid[row, 3].CellValueType = typeof(System.Int32);
							this._uGrid.Grid[row, 3].CellType = "TextBox";
							this._uGrid.Grid[row, 3].ReadOnly = true;
							break;
						case "1":
							dt = GetDeptDataSource();
							this._uGrid.Grid.IgnoreReadOnly = true;
							this._uGrid.Grid[row, 3].ReadOnly = false;
							this._uGrid.Grid.IgnoreReadOnly = false;

							this._uGrid.Grid[row, 3].CellValueType = typeof(System.Int32);
							this._uGrid.Grid[row, 3].CellType = "ComboBox";

							this._uGrid.Grid[row, 3].DataSource = dt;
							this._uGrid.Grid[row, 3].DisplayMember = "Name";
							this._uGrid.Grid[row, 3].ValueMember = "ID";
							this._uGrid.Grid[row, 3].DropDownStyle = GridDropDownStyle.Exclusive;
							break;
						case "2":
							dt = GetFormDataSource();
							this._uGrid.Grid.IgnoreReadOnly = true;
							this._uGrid.Grid[row, 3].ReadOnly = false;
							this._uGrid.Grid.IgnoreReadOnly = false;

							this._uGrid.Grid[row, 3].CellValueType = typeof(System.Int32);
							this._uGrid.Grid[row, 3].CellType = "ComboBox";

							this._uGrid.Grid[row, 3].DataSource = dt;
							this._uGrid.Grid[row, 3].DisplayMember = "Name";
							this._uGrid.Grid[row, 3].ValueMember = "FormID";
							this._uGrid.Grid[row, 3].DropDownStyle = GridDropDownStyle.Exclusive;
							break;
					}
				}

			}
		}

		protected override void grd_CurrentCellValidating(object sender, CancelEventArgs e)
		{
			Console.WriteLine("grd_CurrentCellValidating child");
			base.grd_CurrentCellValidating(sender, e);
		}


		protected override void grd_CurrentCellValidated(object sender, EventArgs e)
		{
			Console.WriteLine("grd_CurrentCellValidated child");
			//base.grd_CurrentCellValidated (sender, e);

			//current cell, row and col index
			GridCurrentCell cc = this._uGrid.Grid.CurrentCell;
			int col = cc.ColIndex;
			int row = cc.RowIndex;

			string valueType = this._uGrid.Grid[row, col].CellValueType.ToString();
			string cellType = this._uGrid.Grid[row, col].CellType;

			Console.WriteLine("\tCellValueType: " + valueType);
			Console.WriteLine("\tCellCellType: " + cellType);
			Console.WriteLine("\tCellValue: " + this._uGrid.Grid[row, col].CellValue);
			Console.WriteLine("\tCellText: " + this._uGrid.Grid[row, col].Text);
			Console.WriteLine("\tCellFormatedText: " + this._uGrid.Grid[row, col].FormattedText);
			Console.WriteLine("\tRenderer ControlValue: " + cc.Renderer.ControlValue);
			Console.WriteLine("\tRenderer ControlText: " + cc.Renderer.ControlText);

			//we want to control the item cell type based on the 
			//value of hierarchy type (only if it's changed)			
			if (col == 2)
			{
				string cellVal = "";
				string rendVal = cc.Renderer.ControlValue.ToString();
				if (this._uGrid.Grid[row, col].CellValue != null)
				{
					cellVal = this._uGrid.Grid[row, col].CellValue.ToString();
					if (cellVal == rendVal)
						return;
					Console.WriteLine("Col 2 has changed");
				}

				DataTable dt = null;
				switch (rendVal)
				{
					case "-9":
						break;
					case "0":
						this._uGrid.Grid[row, col + 1].CellValueType = typeof(System.Int32);
						this._uGrid.Grid[row, col + 1].CellValue = 0;
						this._uGrid.Grid[row, col + 1].CellType = "TextBox";
						this._uGrid.Grid[row, col + 1].ReadOnly = true;
						break;
					case "1":
						dt = GetDeptDataSource();
						this._uGrid.Grid.IgnoreReadOnly = true;
						this._uGrid.Grid[row, col + 1].ReadOnly = false;
						this._uGrid.Grid.IgnoreReadOnly = false;

						this._uGrid.Grid[row, col + 1].CellValueType = typeof(System.Int32);
						this._uGrid.Grid[row, col + 1].CellValue = 0;
						this._uGrid.Grid[row, col + 1].CellType = "ComboBox";

						this._uGrid.Grid[row, col + 1].DataSource = dt;
						this._uGrid.Grid[row, col + 1].DisplayMember = "Name";
						this._uGrid.Grid[row, col + 1].ValueMember = "ID";
						this._uGrid.Grid[row, col + 1].DropDownStyle = GridDropDownStyle.Exclusive;
						break;
					case "2":
						dt = GetFormDataSource();
						this._uGrid.Grid.IgnoreReadOnly = true;
						this._uGrid.Grid[row, col + 1].ReadOnly = false;
						this._uGrid.Grid.IgnoreReadOnly = false;

						this._uGrid.Grid[row, col + 1].CellValueType = typeof(System.Int32);
						this._uGrid.Grid[row, col + 1].CellValue = 0;
						this._uGrid.Grid[row, col + 1].CellType = "ComboBox";

						this._uGrid.Grid[row, col + 1].DataSource = dt;
						this._uGrid.Grid[row, col + 1].DisplayMember = "Name";
						this._uGrid.Grid[row, col + 1].ValueMember = "FormID";
						this._uGrid.Grid[row, col + 1].DropDownStyle = GridDropDownStyle.Exclusive;
						break;
				}

				//assign value to the dataColumn
				DataRow dr = (DataRow)this._uGrid.Grid[row, 0].Tag;
				DataColumn dc = (DataColumn)this._uGrid.Grid[0, col + 1].Tag;
				dr[dc.Ordinal] = this._uGrid.Grid[row, col + 1].CellValue;
			}
		}

		private DataTable GetDeptDataSource()
		{
			StringBuilder sb = new StringBuilder();
			string sql = "SELECT ID, Name FROM Departments";
			//SqlConnection conn = LoginForm.GetSqlConnection();
			DataSet ds = null;
			try
			{
				ds = DataAccess.GetDataSet(sql);
				return ds.Tables[0];
			}
			catch (Exception ex)
			{
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Could get departments.");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Get Departments Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		private DataTable GetFormDataSource()
		{
			StringBuilder sb = new StringBuilder();
			string sql = "SELECT FormID, Name FROM Forms ORDER BY Name";

			//SqlConnection conn = LoginForm.GetSqlConnection();
			DataSet ds = null;
			try
			{
				ds = DataAccess.GetDataSet(sql);
				return ds.Tables[0];
			}
			catch (Exception ex)
			{
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				sb.Append("Could get forms.");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				MessageBox.Show(this._form, sb.ToString(), "Get Forms Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}
	}
}
